Skip to content

Analytics: Track Annotation workflow and edit-mode actions with PostHog events#1760

Merged
LeonardoRosaa merged 12 commits into
mainfrom
leonardo-lig-10227-analytics-track-workflows-data-annotation
Jul 24, 2026
Merged

Analytics: Track Annotation workflow and edit-mode actions with PostHog events#1760
LeonardoRosaa merged 12 commits into
mainfrom
leonardo-lig-10227-analytics-track-workflows-data-annotation

Conversation

@LeonardoRosaa

@LeonardoRosaa LeonardoRosaa commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

What has changed and why?

Instruments nine PostHog events that cover the Annotation workflow and edit-mode interactions.

annotation_tool_selected - fires when the user activates the bounding-box or brush tool; captures collection_id, tool, and triggered_by ("click" or "keyboard_shortcut")
annotation_draw_started - fires on the first canvas mousedown after a drawing tool is selected; captures collection_id, tool, and parent_sample_type
annotation_created - fires in the onSuccess callback of the create mutation; captures collection_id, annotation_type, parent_sample_type, and label_name
annotation_label_updated - fires in the onSuccess callback of the update mutation when exactly one annotation label is changed; captures collection_id and label_name
annotations_bulk_labeled - fires when a label is applied to multiple annotations at once; captured in two places: useUpdateAnnotationsMutation (when the batch contains more than one label update) and SelectedAnnotations (on the bulk-apply action from the annotation grid panel); captures collection_id and annotation_count
annotation_deleted - fires in the onSuccess callback of the delete mutation; captures collection_id and annotation_type
edit_mode_started - fires when the user enters edit mode via the "Edit annotations" button or the keyboard toggle key; captures collection_id and triggered_by
edit_mode_finished - fires when the user exits edit mode via the "Finish Editing" button or the keyboard toggle key; captures collection_id and triggered_by
edit_undo - fires when the user undoes an action via the Undo button or Cmd+Z / Ctrl+Z; only fires when a reversible action actually exists; captures collection_id and triggered_by
annotation_focused - fires when the user focuses an annotation (selects it), either by clicking on it directly in the canvas or by clicking its row in the side panel; not fired on deselect; captures collection_id, annotation_type, focus_source ("canvas" or "side_panel"), and label_name

How has it been tested?

Unit tests

Did you update CHANGELOG.md?

  • Yes
  • Not needed (internal change)

Summary by CodeRabbit

Summary by CodeRabbit

  • Bug Fixes

    • Improved annotation deletion and undo by ensuring deletions are correctly scoped using annotation type (classification, segmentation, object detection, and video events).
  • Improvements

    • Expanded analytics for annotation creation, deletion, labeling, drawing start, annotation focus, tool selection, edit mode, and undo.
    • Tracking now differentiates between actions triggered by clicks vs keyboard shortcuts.
  • Tests

    • Updated and added hook/undo-stack tests to cover the expanded analytics and deletion requirements.

@LeonardoRosaa
LeonardoRosaa requested a review from a team as a code owner July 23, 2026 18:07
@coderabbitai

coderabbitai Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Annotation workflows now emit PostHog events for creation, deletion, labeling, editing, focus, tool selection, and drawing. Annotation deletion APIs and undo flows pass annotation types explicitly.

Changes

Annotation analytics and deletion

Layer / File(s) Summary
Typed annotation deletion
lightly_studio_view/src/lib/components/..., lightly_studio_view/src/lib/hooks/useDeleteAnnotation/*, lightly_studio_view/src/lib/hooks/useSegmentationMaskBrush*, lightly_studio_view/src/lib/services/addAnnotationCreateToUndoStack*
Deletion callers and undo actions pass annotation types; useDeleteAnnotation tracks successful deletions.
Annotation mutation analytics
lightly_studio_view/src/lib/hooks/*, lightly_studio_view/src/routes/+layout.svelte
Creation and update hooks emit lifecycle and label-change events, with tests covering event payloads and shared hook exports.
Editing interaction analytics
lightly_studio_view/src/lib/components/Header/Header.svelte
Edit-mode and undo actions track whether they were triggered by clicks or keyboard shortcuts.
Tool, focus, and drawing analytics
lightly_studio_view/src/lib/components/SampleDetails/*, lightly_studio_view/src/lib/hooks/useAnnotationSelection/*
Tool selection and annotation focus events include trigger sources, while drawing events are guarded to fire once per interaction.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant AnnotationUI
  participant AnnotationHook
  participant PostHog
  AnnotationUI->>AnnotationHook: create, update, focus, select, or delete annotation
  AnnotationHook->>PostHog: trackEvent(annotation interaction)
  PostHog-->>AnnotationHook: tracking completes
Loading

Possibly related PRs

Suggested reviewers: jonaswurst

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: PostHog analytics for annotation workflow and edit-mode actions.
Description check ✅ Passed All required sections are present and the change, testing, and changelog status are documented, though testing details are minimal.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch leonardo-lig-10227-analytics-track-workflows-data-annotation

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@lightly-fast-track-bot

lightly-fast-track-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown

❌  Fast Track: checks did not pass

Guardrail Result Message
dummy Always passes.
frontend/complexity lightly_studio_view/src/lib/components/SampleDetails/SampleSegmentationMaskRect/SampleSegmentationMaskRect.svelte:337 — Arrow function has a complexity of 12. Maximum allowed is 10. lightly_studio_view/src/lib/hooks/useSegmentationMaskBrush.ts:65 — Async arrow function has a complexity of 21. Maximum allowed is 10.
backend/complexity 0 file(s) checked.
backend/coverage 0 file(s) checked.
diff-size PR adds 312 line(s), which exceeds the limit of 215.
frontend/coverage [FAIL] lightly_studio_view/src/lib/components/AnnotationDetails/AnnotationDetailsPanel/AnnotationDetailsPanel.svelte: no test file found [FAIL] lightly_studio_view/src/lib/components/SampleDetails/SampleDetailsPanel.svelte: no test file found [FAIL] lightly_studio_view/src/lib/components/SampleDetails/SampleEraserRect/SampleEraserRect.svelte: no test file found [FAIL] lightly_studio_view/src/lib/components/SampleDetails/SampleObjectDetectionRect/SampleObjectDetectionRect.svelte: no test file found [FAIL] lightly_studio_view/src/lib/components/SampleDetails/SampleSegmentationMaskRect/SampleSegmentationMaskRect.svelte: no test file found [FAIL] lightly_studio_view/src/lib/hooks/index.ts: no test file found [FAIL] lightly_studio_view/src/lib/components/Header/Header.svelte: coverage data not found [FAIL] lightly_studio_view/src/lib/components/SampleDetails/BrushToolPopUp/BrushToolPopUp.svelte: coverage data not found [FAIL] lightly_studio_view/src/lib/components/SampleDetails/SampleDetailsAnnotationSegment/SampleDetailsAnnotationSegment.svelte: coverage data not found [FAIL] lightly_studio_view/src/lib/components/SampleDetails/SampleDetailsClassificationSegment/SampleDetailsClassificationSegment.svelte: coverage data not found [FAIL] lightly_studio_view/src/lib/components/SampleDetails/SampleDetailsToolbar/SampleDetailsToolbar.svelte: coverage data not found [FAIL] lightly_studio_view/src/lib/components/VideoDetails/VideoDetails.svelte: coverage data not found [FAIL] lightly_studio_view/src/lib/hooks/useAnnotationSelection/useAnnotationSelection.ts: coverage data not found [FAIL] lightly_studio_view/src/lib/hooks/useCreateAnnotation/useCreateAnnotation.ts: coverage data not found [FAIL] lightly_studio_view/src/lib/hooks/useDeleteAnnotation/useDeleteAnnotation.ts: coverage data not found [FAIL] lightly_studio_view/src/lib/hooks/useSegmentationMaskBrush.ts: coverage data not found [FAIL] lightly_studio_view/src/lib/hooks/useUpdateAnnotationsMutation/useUpdateAnnotationsMutation.ts: coverage data not found [FAIL] lightly_studio_view/src/lib/services/addAnnotationCreateToUndoStack.ts: coverage data not found

View the guardrail run

To run the guardrails locally, from fast_track/ run make install once, then make run-guardrails (or GUARDRAILS=<name1>,<name2> make run-guardrails for some guardrails).

Reflects a652cff.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@lightly_studio_view/src/lib/components/Header/Header.svelte`:
- Around line 60-68: Move the edit_undo trackEvent call in the keyboard-shortcut
branch after executeReversibleAction(action.id) fulfills, and apply the same
ordering to the other reversible-action path around the referenced lines.
Preserve the existing failure handling so rejected actions do not emit undo
analytics.

In
`@lightly_studio_view/src/lib/hooks/useCreateAnnotation/useCreateAnnotation.test.ts`:
- Around line 15-18: Hoist the trackEvent spy before the Vitest mock factories
execute by defining it through vi.hoisted in both
lightly_studio_view/src/lib/hooks/useCreateAnnotation/useCreateAnnotation.test.ts:15-18
and
lightly_studio_view/src/lib/hooks/useUpdateAnnotationsMutation/useUpdateAnnotationsMutation.test.ts:10-13;
update each usePostHog mock to reuse the hoisted spy, or alternatively convert
the mocks to vi.doMock.

In `@lightly_studio_view/src/lib/hooks/usePostHog.ts`:
- Around line 65-69: Remove the unconditional console.log from trackEvent in
usePostHog; keep posthog.capture(eventName, properties) unchanged so analytics
events continue without exposing payloads in the browser console.

In
`@lightly_studio_view/src/lib/hooks/useUpdateAnnotationsMutation/useUpdateAnnotationsMutation.ts`:
- Around line 31-43: Keep the success-path annotations_bulk_labeled tracking in
useUpdateAnnotationsMutation, and remove the duplicate pre-mutation tracking
from SelectedAnnotations.svelte; update the handler wiring there accordingly so
the event is emitted only after persistence succeeds. Affected sites:
lightly_studio_view/src/lib/hooks/useUpdateAnnotationsMutation/useUpdateAnnotationsMutation.ts
lines 31-43 require no direct change;
lightly_studio_view/src/lib/components/AnnotationsGrid/SelectedAnnotations/SelectedAnnotations.svelte
lines 23-29 must remove the pre-mutation call or rename it to a distinct intent
event, and line 51 must be updated to match the handler change.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 91089e24-7720-485c-83be-c4fd6bce7f1b

📥 Commits

Reviewing files that changed from the base of the PR and between 266526e and 3e059a3.

📒 Files selected for processing (15)
  • lightly_studio_view/src/lib/components/AnnotationDetails/AnnotationDetailsPanel/AnnotationDetailsPanel.svelte
  • lightly_studio_view/src/lib/components/AnnotationsGrid/SelectedAnnotations/SelectedAnnotations.svelte
  • lightly_studio_view/src/lib/components/Header/Header.svelte
  • lightly_studio_view/src/lib/components/SampleDetails/BrushToolPopUp/BrushToolPopUp.svelte
  • lightly_studio_view/src/lib/components/SampleDetails/SampleDetailsAnnotationSegment/SampleDetailsAnnotationSegment.svelte
  • lightly_studio_view/src/lib/components/SampleDetails/SampleDetailsToolbar/SampleDetailsToolbar.svelte
  • lightly_studio_view/src/lib/components/SampleDetails/SampleObjectDetectionRect/SampleObjectDetectionRect.svelte
  • lightly_studio_view/src/lib/components/SampleDetails/SampleSegmentationMaskRect/SampleSegmentationMaskRect.svelte
  • lightly_studio_view/src/lib/hooks/useCreateAnnotation/useCreateAnnotation.test.ts
  • lightly_studio_view/src/lib/hooks/useCreateAnnotation/useCreateAnnotation.ts
  • lightly_studio_view/src/lib/hooks/useDeleteAnnotation/useDeleteAnnotation.test.ts
  • lightly_studio_view/src/lib/hooks/useDeleteAnnotation/useDeleteAnnotation.ts
  • lightly_studio_view/src/lib/hooks/usePostHog.ts
  • lightly_studio_view/src/lib/hooks/useUpdateAnnotationsMutation/useUpdateAnnotationsMutation.test.ts
  • lightly_studio_view/src/lib/hooks/useUpdateAnnotationsMutation/useUpdateAnnotationsMutation.ts

Comment thread lightly_studio_view/src/lib/components/Header/Header.svelte Outdated
Comment thread lightly_studio_view/src/lib/hooks/useCreateAnnotation/useCreateAnnotation.test.ts Outdated
Comment thread lightly_studio_view/src/lib/hooks/usePostHog.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3e059a3118

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread lightly_studio_view/src/lib/hooks/useDeleteAnnotation/useDeleteAnnotation.ts Outdated
Comment thread lightly_studio_view/src/lib/hooks/usePostHog.ts Outdated
Comment thread lightly_studio_view/src/lib/components/Header/Header.svelte Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@lightly_studio_view/src/lib/hooks/useAnnotationSelection/useAnnotationSelection.ts`:
- Around line 51-58: Use the shared annotation_focused capture in
useAnnotationSelection for both focus sources, and remove the duplicate
analytics path from SampleDetailsAnnotationSegment: delete the unused usePostHog
import, remove trackEvent destructuring, and remove the local capture block.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: f2a73297-94a6-4af8-997a-d073ca92fe85

📥 Commits

Reviewing files that changed from the base of the PR and between 904d805 and b34aef8.

📒 Files selected for processing (3)
  • lightly_studio_view/src/lib/components/SampleDetails/SampleDetailsAnnotationSegment/SampleDetailsAnnotationSegment.svelte
  • lightly_studio_view/src/lib/components/SampleDetails/SampleDetailsPanel.svelte
  • lightly_studio_view/src/lib/hooks/useAnnotationSelection/useAnnotationSelection.ts

@LeonardoRosaa

Copy link
Copy Markdown
Contributor Author

/review

@IgorSusmelj IgorSusmelj left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@LeonardoRosaa
LeonardoRosaa added this pull request to the merge queue Jul 24, 2026
Merged via the queue into main with commit a660790 Jul 24, 2026
21 checks passed
@LeonardoRosaa
LeonardoRosaa deleted the leonardo-lig-10227-analytics-track-workflows-data-annotation branch July 24, 2026 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants